﻿namespace aoteman
{

    /// <summary>
    /// 奥特曼类
    /// </summary>
    struct Otm
    {
        public string name;
        public int posx;
        public int posy;
        public string icon;
        public int blood;
        public int attack;
        public Otm(string name, int posx, int posy, string icon,int blood,int attack)
        {
            this.name = name;
            this.posx = posx;
            this.posy = posy;
            this.icon = icon;
            this.attack = attack;
            this.blood = blood;
        }

        /// <summary>
        /// 绘制奥特曼
        /// </summary>
        public void DrawIcon()
        {
            Console.SetCursorPosition(posx, posy);
            Console.Write(icon);
        }

        /// <summary>
        /// 清除奥特曼
        /// </summary>
        public void ClearIcon()
        {
            Console.SetCursorPosition(posx, posy);
            Console.Write(" ");
        }

        /// <summary>
        /// 移动奥特曼
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void Move(int x, int y)
        {
            ClearIcon(); // 清除原位置的图标
            posx = x; // 更新位置
            posy = y; // 更新位置
            // 检查边界
            if (posx <= 1) posx = 1;
            if (posy <= 1) posy = 1;
            if (posx >= 30) posx = 30; // 假设地图宽度为30
            if (posy >= 14) posy = 14; // 假设地图高度为15
            DrawIcon(); // 绘制新位置的图标
        }

        /// <summary>
        /// 攻击怪兽
        /// </summary>
        /// <param name="monster1"></param>
        public void Attack(ref Monster monster1)
        {
            monster1.blood -= attack; // 减少怪兽的血量
            if (monster1.blood < 0) monster1.blood = 0; // 血量不能为负数
            //Console.SetCursorPosition(0, 25);
            //Console.WriteLine($"{name} 攻击了 {monster1.name}，造成了 {attack} 点伤害！");
            if (monster1.blood <= 0)
            {
                Console.SetCursorPosition(10, 17);
                Console.Write($"{monster1.name} 被打败了！");
            }
        }

        public void ShowInfo()
        {
            Console.SetCursorPosition(0, 17);
            Console.Write($"姓名：{name}");
            Console.SetCursorPosition(0, 18);
            Console.Write($"位置：({posx}, {posy})");
            Console.SetCursorPosition(0, 19);
            Console.Write($"血量：{blood}  ");
            Console.SetCursorPosition(0, 20);
            Console.Write($"攻击力：{attack}");
        }
    }


    /// <summary>
    /// 怪物类
    /// </summary>
    struct Monster
    {
        public string name;
        public int posx;
        public int posy;
        public string icon;
        public int blood;
        public int attack;
        public Monster(string name, int posx, int posy, string icon,int blood,int attack)
        {
            this.name = name;
            this.posx = posx;
            this.posy = posy;
            this.icon = icon;
            this.attack = attack;
            this.blood = blood;
        }

        /// <summary>
        /// 绘制怪物
        /// </summary>
        public void DrawIcon()
        {
            Console.SetCursorPosition(posx, posy);
            Console.Write(icon);
        }

        /// <summary>
        /// 清除怪兽
        /// </summary>
        public void ClearIcon()
        {
            Console.SetCursorPosition(posx, posy);
            Console.Write(" ");
        }

        /// <summary>
        /// 移动怪兽
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void Move(int x, int y)
        {
            ClearIcon(); // 清除原位置的图标
            posx = x; // 更新位置
            posy = y; // 更新位置
            // 检查边界
            if (posx <= 1) posx = 1;
            if (posy <= 1) posy = 1;
            if (posx >= 30) posx = 30; // 假设地图宽度为30
            if (posy >= 14) posy = 14; // 假设地图高度为15
            DrawIcon(); // 绘制新位置的图标
        }

        public void ShowInfo()
        {
            Console.SetCursorPosition(25, 17);
            Console.Write($"姓名：{name}");
            Console.SetCursorPosition(25, 18);
            Console.Write($"位置：({posx}, {posy})");
            Console.SetCursorPosition(25, 19);
            Console.Write($"血量：{blood}  ");
            Console.SetCursorPosition(25, 20);
            Console.Write($"攻击力：{attack}");
        }
    }


    internal class Program
    {
        static void Main(string[] args)
        {

            ShowMap();
            Otm otm1 = new Otm("奥特曼", 10, 10, "O",100,6);
            otm1.DrawIcon(); // 绘制奥特曼图标
            otm1.ShowInfo(); // 显示奥特曼信息
            Monster monster1 = new Monster("怪兽", 5, 5, "M", 50, 5);
            monster1.DrawIcon(); // 绘制怪兽图标
            monster1.ShowInfo(); // 显示怪兽信息
            while (true)
            {
                // 移动奥特曼
                Move(ref otm1);
                // 碰撞检测
                CrashCheck(ref otm1, ref monster1);
            }
        }

        /// <summary>
        /// 检查是否与怪兽相撞
        /// </summary>
        /// <param name="otm1"></param>
        /// <param name="monster1"></param>
        private static void CrashCheck(ref Otm otm1, ref Monster monster1)
        {
            // 检查是否与怪兽相撞
            if (Math.Abs(otm1.posx - monster1.posx) == 1 && otm1.posy == monster1.posy)
            {
                //Console.SetCursorPosition(0, 25);
                //Console.WriteLine("你撞上了怪兽！");
                Fire(ref otm1, ref monster1);
            }
            else if (otm1.posx == monster1.posx && Math.Abs(otm1.posy - monster1.posy) == 1)
            {
                //Console.SetCursorPosition(0, 25);
                //Console.WriteLine("你撞上了怪兽！");
                Fire(ref otm1, ref monster1);
            }
            else
            {
                //Console.SetCursorPosition(0, 25);
                //Console.WriteLine("你没有撞上怪兽！");
            }
        }

        /// <summary>
        /// 奥特曼攻击怪兽
        /// </summary>
        /// <param name="otm1"></param>
        /// <param name="monster1"></param>
        public static void Fire(ref Otm otm1, ref Monster monster1)
        {
            while(true)
            {
                // 显示信息
                otm1.ShowInfo();
                monster1.ShowInfo();
                Console.SetCursorPosition(0, 26);
                Console.WriteLine("按下空格键攻击怪兽！");
                char input = Console.ReadKey().KeyChar;
                if (input == ' ')
                {
                    otm1.Attack(ref monster1);
                    if (monster1.blood <= 0)
                    {
                        Console.SetCursorPosition(0, 27);
                        Console.WriteLine("怪兽已被打败！");
                        monster1.ClearIcon(); // 清除怪兽图标
                        break;
                    }
                    else
                    {
                        Console.SetCursorPosition(0, 27);
                        Console.WriteLine($"怪兽剩余血量：{monster1.blood}");
                    }
                }
            }
        }

        /// <summary>
        /// 移动奥特曼
        /// </summary>
        /// <param name="otm1"></param>
        private static void Move(ref Otm otm1)
        {
            char input = Console.ReadKey().KeyChar;
            // 清除输入的字符
            Console.Write("\b \b");
            switch (input)
            {
                case 'w':
                    otm1.Move(otm1.posx, otm1.posy - 1);
                    break;
                case 's':
                    otm1.Move(otm1.posx, otm1.posy + 1);
                    break;
                case 'a':
                    otm1.Move(otm1.posx - 1, otm1.posy);
                    break;
                case 'd':
                    otm1.Move(otm1.posx + 1, otm1.posy);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 打印地图
        /// </summary>
        static void ShowMap()
        {
            Console.WriteLine("---------------------------------");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("|                               |");
            Console.WriteLine("---------------------------------");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("|          |            |       |");
            Console.WriteLine("---------------------------------");
        }
    }
}
